package fi.profecons.csswindows;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import fi.profecons.csswindows.toolkit.ContentPane;
import fi.profecons.csswindows.toolkit.Menu;

public class RestrictedContainer extends Element{
	
	private Menu menu;
	
	private Vector elements;

	public RestrictedContainer() {
		this.elements = new Vector();
	}

	protected void setState(State state) {
		Iterator i = getAll().iterator();
		while (i.hasNext()) {
			Element element = (Element) i.next();
			element.setState(state);
		}
		super.setState(state);
	}

	/**
	 * Get the complete list of elements in this container.
	 */
	protected Vector getAll() {
		return this.elements;
	}
	
	public Element getElement(String id) {
		if (getId().equals(id)) {
			return this;
		}
		Iterator i = getAll().iterator();
		while (i.hasNext()) {
			Element element = (Element) i.next();
			Element found = element.getElement(id);
			if (found != null) {
				return found;
			}
		}
		return null;
	}
	
	public Menu getMenu() {
		return menu;
	}

	public void setMenu(Menu menu) {
		this.menu = menu;
		getAll().add(menu);
		menu.setState(getState());
		addCommand("getElement('" + getId() + "').setMenu(getElement('"
				+ menu.getId() + "'));");
	}
	
	protected void add(Element element){
		elements.add(element);
		element.setState(getState());
	}
}
